package gui;

import controllers.GUIController;
import hibernate.User;
import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.event.*;

@SuppressWarnings("serial")
public class AMyClass extends JInternalFrame {

    private JPanel buttonPanel;
    private JButton homeButton;
    private JPanel bkgrd;
    private JPanel rosterPanel;
    private JLabel logo;
    private JPanel logoPanel;
    private JButton logoutButton;
    private JButton myaccountButton;
    private JButton activitiesButton;
    private JButton launchButton;
    private JList studentList;
    private JScrollPane scroll;
    private GUIController guiC;
    final AMyClass obj = this;
    private User currentStudent;
    //private User currentTeacher;

    public AMyClass(GUIController m) {
        guiC = m;
        //currentTeacher = guiC.currentUser;
        generateLayout();
        addMyAccountActionListener();
        addHomeActionListener();
        addLogoutActionListener();
        addLaunchActionListener();
        addActivitiesActionListener();
        addStudentListSelectionListener();

        updateStudentList();

    }

    // adds actionlistener to the logout button to log the admin out of their
    // account
    private void addLogoutActionListener() {
        logoutButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // show login page/close application on logout
                guiC.showGUI(new ALogin(guiC));
                // close previous gui(AMyClass)
                obj.dispose();
            }
        });

    }

    // adds actionlistener to myaccount button to go to the page My Class
    private void addMyAccountActionListener() {
        myaccountButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    guiC.showGUI(new AMyAccount(guiC));
                } catch (IOException ex) {
                    Logger.getLogger(AMyClass.class.getName()).log(Level.SEVERE, null, ex);
                }
                obj.dispose();
            }
        });
    }

    //adds actionlistener to home button to go to the home page
    private void addHomeActionListener() {
        homeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new AMyClass(guiC));
                obj.dispose();
            }
        });
    }

    private void addActivitiesActionListener() {
        activitiesButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new AAddActivity(guiC));
                obj.dispose();
            }
        });
    }

    // with the software
    private void addLaunchActionListener() {
        launchButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                guiC.showGUI(new StudentView(guiC));
            }
        });
    }

    private void addStudentListSelectionListener() {
        studentList.addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                User u = (User) guiC.currentUser.getStudentList().get(studentList.getSelectedIndex());
                currentStudent = u;
                showSelectedStudent(currentStudent);
            }
        });
    }

    //user is a student
    //given the student, will link the selected index to the appropriate 
    //student's page
    private void showSelectedStudent(User u) {
        guiC.showGUI(new AStudentSched(guiC, u));
        obj.dispose();
    }

    private void updateStudentList() {
        DefaultListModel model = new DefaultListModel();
        User u;
        guiC.currentUser.setStudentList(guiC.getStudentList(guiC.currentUser));
        int count = 0;
        for (Iterator i = guiC.currentUser.getStudentList().iterator(); i.hasNext();) {
            u = (User) i.next();
            model.add(count++, u.getFirstName() + " " + u.getLastName());
        }
        studentList.setModel(model);
        addStudentListSelectionListener();
    }

    private void generateLayout() {
        bkgrd = new JPanel();
        bkgrd.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        logoPanel = new JPanel();
        logo = new JLabel();
        buttonPanel = new JPanel();
        homeButton = new JButton("Home");
        myaccountButton = new JButton("My Account");
        activitiesButton = new JButton("Activities");
        logoutButton = new JButton("Logout");
        rosterPanel = new JPanel(new GridBagLayout());
        launchButton = new JButton("Launch");
        launchButton.setIcon(new ImageIcon(AMyClass.class.getResource("/gui/check.png")));
        studentList = new JList();

        //changes made
        scroll = new JScrollPane();
        studentList.setName("Class Students");
        studentList.setVisibleRowCount(8);
        scroll.setViewportView(studentList);
        scroll.setSize(new Dimension(500, 500));
        rosterPanel.add(scroll);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setTitle("OnPoint Scheduler- My Class");


        bkgrd.setBackground(new java.awt.Color(0, 102, 255));
        bkgrd.setToolTipText("");

        logoPanel.setOpaque(false);

        logo.setIcon(new ImageIcon(getClass().getResource(
                "/gui/OnPointlogo2-1.png")));
        // NOI18N

        GroupLayout logoPanelLayout = new GroupLayout(
                logoPanel);
        logoPanel.setLayout(logoPanelLayout);
        logoPanelLayout.setHorizontalGroup(logoPanelLayout.createParallelGroup(
                GroupLayout.Alignment.LEADING).addGroup(
                logoPanelLayout.createSequentialGroup().addContainerGap().addComponent(logo).addContainerGap(23, Short.MAX_VALUE)));
        logoPanelLayout.setVerticalGroup(logoPanelLayout.createParallelGroup(
                GroupLayout.Alignment.LEADING).addComponent(logo,
                GroupLayout.Alignment.TRAILING,
                GroupLayout.DEFAULT_SIZE,
                GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

        buttonPanel.setOpaque(false);

        homeButton.setOpaque(false);
        activitiesButton.setOpaque(false);
        myaccountButton.setOpaque(false);

        logoutButton.setOpaque(false);

        GroupLayout buttonPanelLayout = new GroupLayout(
                buttonPanel);
        buttonPanel.setLayout(buttonPanelLayout);
        buttonPanelLayout.setHorizontalGroup(buttonPanelLayout.createParallelGroup(
                GroupLayout.Alignment.LEADING).addGroup(
                buttonPanelLayout.createSequentialGroup().addContainerGap().addGroup(
                buttonPanelLayout.createParallelGroup(
                GroupLayout.Alignment.LEADING,
                false).addComponent(
                myaccountButton,
                GroupLayout.DEFAULT_SIZE,
                GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE).addComponent(
                homeButton,
                GroupLayout.DEFAULT_SIZE,
                GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE).addComponent(
                activitiesButton,
                GroupLayout.DEFAULT_SIZE,
                GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE).addComponent(
                logoutButton,
                GroupLayout.DEFAULT_SIZE,
                GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE)).addContainerGap(
                GroupLayout.DEFAULT_SIZE,
                Short.MAX_VALUE)));
        buttonPanelLayout.setVerticalGroup(buttonPanelLayout.createParallelGroup(
                GroupLayout.Alignment.LEADING).addGroup(
                buttonPanelLayout.createSequentialGroup().addContainerGap().addComponent(homeButton).addPreferredGap(
                LayoutStyle.ComponentPlacement.RELATED).addPreferredGap(
                LayoutStyle.ComponentPlacement.RELATED).addComponent(myaccountButton).addPreferredGap(
                LayoutStyle.ComponentPlacement.RELATED).addComponent(activitiesButton).addPreferredGap(
                LayoutStyle.ComponentPlacement.RELATED).addComponent(logoutButton).addContainerGap(42, Short.MAX_VALUE)));

        rosterPanel.setBackground(new java.awt.Color(51, 153, 255));
        rosterPanel.setBorder(BorderFactory.createTitledBorder(null,
                "My Class",
                javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
                javax.swing.border.TitledBorder.DEFAULT_POSITION,
                new java.awt.Font("Aharoni", 1, 32))); // NOI18N



        // GroupLayout gl_rosterPanel = new GroupLayout(rosterPanel);

        GroupLayout gl_bkgrd = new GroupLayout(
                bkgrd);
        gl_bkgrd.setHorizontalGroup(
                gl_bkgrd.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_bkgrd.createSequentialGroup()
                .addContainerGap()
                .addGroup(gl_bkgrd.createParallelGroup(Alignment.LEADING)
                .addComponent(logoPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addGroup(gl_bkgrd.createParallelGroup(Alignment.TRAILING)
                .addComponent(launchButton)
                .addGroup(gl_bkgrd.createSequentialGroup()
                .addComponent(buttonPanel, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(rosterPanel, GroupLayout.PREFERRED_SIZE, 674, GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(135, Short.MAX_VALUE)));
        gl_bkgrd.setVerticalGroup(
                gl_bkgrd.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_bkgrd.createSequentialGroup()
                .addContainerGap()
                .addComponent(logoPanel, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addComponent(launchButton)
                .addPreferredGap(ComponentPlacement.RELATED)
                .addGroup(gl_bkgrd.createParallelGroup(Alignment.LEADING)
                .addGroup(gl_bkgrd.createSequentialGroup()
                .addComponent(buttonPanel, GroupLayout.DEFAULT_SIZE, 171, Short.MAX_VALUE)
                .addGap(311))
                .addGroup(gl_bkgrd.createSequentialGroup()
                .addComponent(rosterPanel, GroupLayout.PREFERRED_SIZE, 341, GroupLayout.PREFERRED_SIZE)
                .addContainerGap()))));
        bkgrd.setLayout(gl_bkgrd);

        GroupLayout layout = new GroupLayout(
                getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(
                GroupLayout.Alignment.LEADING).addComponent(
                bkgrd, GroupLayout.DEFAULT_SIZE,
                GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));
        layout.setVerticalGroup(layout.createParallelGroup(
                GroupLayout.Alignment.LEADING).addComponent(
                bkgrd, GroupLayout.DEFAULT_SIZE,
                GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE));

        pack();
    }
}